package edu.pw.p2pclient.data;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

import edu.pw.p2pclient.logic.SharedManager;
import edu.pw.p2pclient.networking.messages.Message;

public class RemoteDeviceInfo {

	private InetAddress mAddress;
	private long mResourcesVersion;
	private ArrayList<Resource> mResources;
	private Socket mSocket;
	private ObjectOutputStream mOos;
	private ObjectInputStream mOis;

	public RemoteDeviceInfo(InetAddress address) {
		mAddress = address;
		mResourcesVersion = SharedManager.UNKNOWN_RESOURCE_LIST_VERSION;
		mResources = null;
		mSocket = null;
		mOos = null;
		mOis = null;
	}

	public RemoteDeviceInfo(InetAddress address, int resourcesVersion,
			ArrayList<Resource> resources) {
		mAddress = address;
		mResourcesVersion = resourcesVersion;
		mResources = resources;
		mSocket = null;
		mOos = null;
		mOis = null;
	}

	public RemoteDeviceInfo(InetAddress address, Socket socket) {
		mAddress = address;
		mResourcesVersion = SharedManager.UNKNOWN_RESOURCE_LIST_VERSION;
		mResources = null;
		mSocket = socket;
		try {
			mOos = new ObjectOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
			mOos = null;
			mSocket = null;
		}
		try {
			mOis = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			mOis = null;
			mSocket = null;
		}
	}

	public RemoteDeviceInfo(InetAddress address, int resourcesVersion,
			ArrayList<Resource> resources, Socket socket) {
		mAddress = address;
		mResourcesVersion = resourcesVersion;
		mResources = resources;
		mSocket = socket;
		try {
			mOos = new ObjectOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
			mOos = null;
			mSocket = null;
		}
		try {
			mOis = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			mOis = null;
			mSocket = null;
		}
	}

	public InetAddress getAddress() {
		return mAddress;
	}

	public long getResourcesVersion() {
		return mResourcesVersion;
	}

	public void setResourcesVersion(long resourcesVersion) {
		mResourcesVersion = resourcesVersion;
	}

	public ArrayList<Resource> getResources() {
		return mResources;
	}

	public void setResources(ArrayList<Resource> resources) {
		mResources = resources;
	}

	public Socket getSocket() {
		return mSocket;
	}

	public void setSocket(Socket socket) throws IOException {
		mSocket = socket;
		if (socket == null) {
			mOos = null;
			mOis = null;
		} else {
			mOos = new ObjectOutputStream(socket.getOutputStream());
			mOis = new ObjectInputStream(socket.getInputStream());
		}
	}

	public void writeObject(Message msg) throws IOException {
		if (mOos != null) {
			synchronized (mOos) {
				mOos.writeObject(msg);
			}
		}
	}

	public ObjectInputStream getObjectInputStream() {
		return mOis;
	}

	public void close() {
		try {
			if (mOis != null)
				mOis.close();
		} catch (IOException ex) {
		}
		try {
			if (mOos != null) {
				synchronized (mOos) {
					mOos.close();
				}
			}
		} catch (IOException ex) {
		}
		try {
			if (mSocket != null)
				mSocket.close();
		} catch (IOException ex) {
		}
	}

	@Override
	public boolean equals(Object o) {

		if (o == null)
			return false;
		if (getClass().equals(InetAddress.class))
			return mAddress.equals((InetAddress) o);
		if (!getClass().equals(o.getClass()))
			return false;
		boolean id = mAddress.equals(((RemoteDeviceInfo) o).getAddress());
		return id;
	}

}
